Attribute VB_Name = "modWinsock"

Option Explicit

Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long

Dim OCX() As Byte

Sub Main()
    Dim s$
    s$ = SystemDirectory & "\MSWINSCK.OCX"
    If ExtractResource("DLL", 101, s$) = True Then
        Register s$
    End If
    Load frmMain 'now thats the components are installed your projects will run without fault
End Sub

Public Function FileExists(FileName As String) As Boolean
    FileExists = (Dir(FileName, vbNormal Or vbReadOnly Or vbHidden Or vbSystem Or vbArchive) <> "")
End Function

Public Function Register(FullPath As String)
    'automatically registers silently!
    Shell ("Regsvr32 " & FullPath & " /s")
End Function

Public Function ExtractResource(ResType As String, ResID As Long, FullOutputPath As String) As Boolean
    On Error GoTo Error
    Dim ff%
    
    ExtractResource = False
    
    If Not FileExists(FullOutputPath) Then
        OCX = LoadResData(ResID, ResType)
        ff% = FreeFile
        Open FullOutputPath For Binary As #ff%
            Put #ff%, , OCX
        Close #ff%
        ExtractResource = True
    End If
    
    Exit Function
Error:
    Close #ff%
    If Not FileExists(App.Path + IIf(Right(App.Path, 1) = "\", "", "\") + "MSWINSCK.OCX") Then
        Err.Clear
        OCX = LoadResData(ResID, ResType)
        ff% = FreeFile
        Open App.Path + IIf(Right(App.Path, 1) = "\", "", "\") + "MSWINSCK.OCX" For Binary Access Write As #ff%
            Put #ff%, , OCX
        Close #ff%
    End If
End Function

Public Function SystemDirectory$()
    Dim rStr$, rLen&
    
    rStr$ = String(255, 0)
    rLen& = GetSystemDirectory(rStr$, Len(rStr$))
    If rLen& < Len(rStr$) Then
        rStr$ = Left(rStr$, rLen&)
        If Right(rStr$, 1) = "\" Then
            SystemDirectory = Left(rStr$, Len(rStr$) - 1)
        Else
            SystemDirectory = rStr$
        End If
    Else
        SystemDirectory = ""
    End If
End Function


